package com.superjson.showpro.dao.impl;

import com.superjson.showpro.base.GenericDaoImpl;
import com.beantools.util.StringUtil;
import com.superjson.showpro.dao.SystemRecordDao;
import com.superjson.showpro.entity.SystemRecord;
import org.hibernate.Query;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

@Repository
public class SystemRecordDaoImpl extends GenericDaoImpl<SystemRecord> implements SystemRecordDao {

    public SystemRecordDaoImpl() {
        super(SystemRecord.class);
    }

    @Override
    public Long countPageView(String startTime, String endTime) {
        String sql = "SELECT " +
                " COUNT(DISTINCT id) AS pageView " +
                "FROM " +
                " sp_system_record " +
                "WHERE " +
                " is_deleted = 0 " +
                (StringUtil.isNotBlank(startTime) ? "AND create_datetime >= '" + startTime + "' " : "") +
                (StringUtil.isNotBlank(endTime) ? "AND create_datetime < '" + endTime + "'" : "");
        Query query = getSession().createSQLQuery(sql).addScalar("pageView", StandardBasicTypes.LONG);
        return (Long) query.uniqueResult();
    }
}